package com.srpingde.hhh;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.springde.hhh.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.srpingde.hhh.FenLeiActivity.FenLeiListViewAdapter.fenLeiHolder;
import com.srpingde.hhh.adapter.TopGridViewAdaPter;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.application.HHHDataManage;
import com.srpingde.hhh.bean.FenLeiData;
import com.srpingde.hhh.bean.GuangGao;
import com.srpingde.hhh.bean.SearchBean;
import com.srpingde.hhh.bean.SearchRowsBean;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class SearchActivity extends Activity {
	ImageView back;
	HHHIml weiQuIml;
	String content;
	TextView opinion_ly;
	GridView gridView;
	SearchBean searchBean;
	List<SearchRowsBean> searchRowsBeans;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        searchRowsBeans = new ArrayList<SearchRowsBean>();
        //初始化和计算ui
        layout();
        if(getIntent().getExtras()!=null){
        	content = getIntent().getStringExtra("search");
        	opinion_ly.setText(content);
        }
        getData();
        gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent in= new Intent(SearchActivity.this,XiangQingActivity.class);
				in.putExtra("id", searchRowsBeans.get(arg2).getPid());
				startActivity(in);
			}
		});
    }
    public void onClick(View v){
    	finish();
    }
    //请求数据
    private void getData(){
    	weiQuIml = new HHHIml();
    	String url = HHHInterface.httpHeader + "api.php?tab=seachproducts&act=data&pagesize=10&page=1&cid=0&keyword="+content;
    	Log.e("mytag",url+"============搜索的url");
		weiQuIml.searchHttp(SearchActivity.this, url,new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				Log.e("mytag", content+"================");
				searchRowsBeans = HHHDataManage.getInstance().getSearchBean().getRows();
				gridView.setAdapter(new SearchViewAdapter(SearchActivity.this,searchRowsBeans));
			}
		});
    }
    private void layout() {
    	back = (ImageView) findViewById(R.id.back);
    	opinion_ly = (TextView) findViewById(R.id.opinion_ly);
    	gridView = (GridView) findViewById(R.id.fenlei_name_gridview);
		// 计算头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv
				.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		//back
		RelativeLayout.LayoutParams backLayoutParams = (LayoutParams) back.getLayoutParams();
		backLayoutParams.height = HhhApp.getInstance().title_h();
		back.setLayoutParams(backLayoutParams);
	}
    class SearchViewAdapter extends BaseAdapter {
		List<SearchRowsBean> fenLeiDatas;
		Context context;
		public SearchViewAdapter(Context context,List<SearchRowsBean> fenLeiDatas) {
			super();
			this.context = context;
			this.fenLeiDatas = fenLeiDatas;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fenLeiDatas.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			fenLeiHolder holder = null;
			if(arg1 == null){
				holder = new fenLeiHolder();
				arg1 = LayoutInflater.from(context).inflate(R.layout.fenlei_item_adapter, null);
				holder.icon = (ImageView) arg1.findViewById(R.id.fenlei_item_icon);
				holder.item_price = (TextView) arg1
						.findViewById(R.id.item_price);
				holder.item_name = (TextView) arg1
						.findViewById(R.id.item_name);
				arg1.setTag(holder);
			}else{
				holder = (fenLeiHolder) arg1.getTag();
			}
			itemLayout(holder.icon);
			holder.item_name.setText(fenLeiDatas.get(arg0).getName());
			holder.item_price.setText("  ￥"
					+ fenLeiDatas.get(arg0).getPrice());
			final ImageView kvShow = holder.icon;
    		ImageLoader.getInstance().loadImage(HHHIml.httpHeader+fenLeiDatas.get(arg0).getUrl(), new SimpleImageLoadingListener(){
    			@Override
    			public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
    				BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
    				//kvShow.setImageBitmap(bitmap);
    				kvShow.setBackgroundDrawable(bitmapDrawable);
    			}
    		});
			//holder.icon.setBackgroundResource(images[arg0]);
			return arg1;
		}
		class fenLeiHolder{
			ImageView icon;
			TextView item_price,item_name;
		}
	}
    private void itemLayout(ImageView imageView){
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mm_fenlei_select);
		int wh = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[1]/2);
		RelativeLayout.LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();
		layoutParams.height = wh;
		layoutParams.width = wh;
		imageView.setLayoutParams(layoutParams);
	}
}
